import {Component} from '@angular/core';
import {CommonModule} from '@angular/common';
import {RouterLink, RouterOutlet} from '@angular/router';
import {NzIconModule} from 'ng-zorro-antd/icon';
import {NzLayoutModule} from 'ng-zorro-antd/layout';
import {NzMenuModule} from 'ng-zorro-antd/menu';
import {AppTokenTaskService} from './server/app-token-task.service';
import {EventsService, EventStrategyDispatcher} from './server/events/Events.service';
import {Subscription} from 'rxjs';
import {IEvent} from './server/events/EventM';


@Component({
  selector: 'app-root',
  standalone: true,
  //页面要用的 组件
  imports: [CommonModule, RouterLink, RouterOutlet, NzIconModule, NzLayoutModule, NzMenuModule],
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.less']
})
export class AppComponent {
  subscription: Subscription;

  constructor(private appTokenTaskServer: AppTokenTaskService,
              private appEventsService: EventsService<IEvent>,
              private eventStrategyHandle: EventStrategyDispatcher<IEvent>
  ) {
    //app中订阅IEvent接口的事件
    this.subscription = this.appEventsService.getEventObservable().subscribe(value => this.eventStrategyHandle.dispatch(value));
  }


  ngOnInit() {
    console.log('AppComponent ngOnInit ');
    // 创建一个定时任务，每分钟执行一次
    this.appTokenTaskServer.startRefreshTokenTask();
  }


  ngOnDestroy() {
    //取消订阅
    this.subscription.unsubscribe()
    console.log('AppComponent ngOnDestroy ');
    this.appTokenTaskServer.stopRefreshTokenTask();
  }


}
